package cn.walls1717.ggkt.wechat.controller;


import cn.walls1717.ggkt.exception.MyException;
import cn.walls1717.ggkt.model.wechat.Menu;
import cn.walls1717.ggkt.result.Result;
import cn.walls1717.ggkt.vo.wechat.MenuVo;
import cn.walls1717.ggkt.wechat.service.MenuService;
import cn.walls1717.ggkt.wechat.util.ConstantPropertiesUtil;
import cn.walls1717.ggkt.wechat.util.HttpClientUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公众号菜单相关接口
 *
 * @author walls1717
 * @since 2022/07/19
 */
@Api(tags = "公众号菜单相关接口")
@RestController
@RequestMapping("/admin/wechat/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("公众号菜单删除")
    @DeleteMapping("removeMenu")
    public Result<?> deleteMenu() {
        menuService.deleteMenu();
        return Result.success();
    }

    @ApiOperation("同步菜单")
    @GetMapping("syncMenu")
    public Result<?> updateWechatMenu() {
        menuService.updateWechatMenu();
        return Result.success();
    }

    @ApiOperation("获取access_token")
    @GetMapping("getAccessToken")
    public Result<String> getAccessToken() {
        try {
            //拼接请求地址
            StringBuffer buffer = new StringBuffer();
            buffer.append("https://api.weixin.qq.com/cgi-bin/token");
            buffer.append("?grant_type=client_credential");
            buffer.append("&appid=%s");
            buffer.append("&secret=%s");
            String url = String.format(buffer.toString(), ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            String tokenString = HttpClientUtils.get(url);
            //获取access_token
            JSONObject jsonObject = JSONObject.parseObject(tokenString);
            String accessToken = jsonObject.getString("access_token");
            //返回
            return Result.success(accessToken);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001, "获取access_token失败");
        }
    }

    @ApiOperation("获取一二级菜单")
    @GetMapping("findMenuInfo")
    public Result<List<MenuVo>> listMenus() {
        List<MenuVo> list = menuService.listMenus();
        return Result.success(list);
    }

    @ApiOperation("获取所有一级菜单")
    @GetMapping("findOneMenuInfo")
    public Result<List<Menu>> listOneMenus() {
        List<Menu> list = menuService.listOneMenus();
        return Result.success(list);
    }


    @ApiOperation("根据id获取")
    @GetMapping("get/{id}")
    public Result<Menu> getMenuById(@PathVariable Long id) {
        Menu menu = menuService.getById(id);
        return Result.success(menu);
    }

    @ApiOperation("新增")
    @PostMapping("save")
    public Result<?> saveMenu(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.success();
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public Result<?> updateMenu(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("remove/{id}")
    public Result<?> deleteMenuById(@PathVariable Long id) {
        menuService.removeById(id);
        return Result.success();
    }

    @ApiOperation("根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result<?> deleteMenusByIds(@RequestBody List<Long> idList) {
        menuService.removeByIds(idList);
        return Result.success();
    }
}

